package com.open.party.cloud.view.redMeeting

import android.graphics.Typeface
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.ArticleEntity
import com.open.party.cloud.model.ArticleVo
import com.open.party.cloud.model.DictionaryBean
import com.open.party.cloud.model.SlideshowListBean
import com.open.party.cloud.view.comm.view.banner.CustomViewHolder
import com.open.party.cloud.view.home.article.ArticleDetailsActivity
import com.open.party.cloud.view.home.article.fragment.ArticleListFragment
import com.open.party.cloud.viewModel.ArticleViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.banner.nice.BannerConfig
import com.sinothk.banner.nice.Transformer
import kotlinx.android.synthetic.main.red_meeting_main.*
import kotlinx.android.synthetic.main.red_meeting_main.bannerView
import kotlinx.android.synthetic.main.red_meeting_main.scrollTab
import kotlinx.android.synthetic.main.red_meeting_main.viewPager
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class RedMeetingMainActivity : AppEventBusTitleBaseActivity() {
    private var viewModel: ArticleViewModel? = null

    override fun getLayoutResId(): Int = R.layout.red_meeting_main

    private fun initViewListData(type: String) {
        val pageReq = PageReq<ArticleVo>()
        pageReq.pageNo = 1
        pageReq.pageSize = 6
        val vo = ArticleVo()
        vo.childrenType = DictionaryBean(type, "")
        pageReq.data = vo

        viewModel!!.getArticleListData(pageReq, type)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun receiveStudentEventBus(result: ResultInfo<AppPageData<ArticleEntity>>) {

        if ("QHJS_LBXW" == result.eventType) {
            when (result.code) {
                ResultInfo.SUCCESS -> {
                    if (result.data != null && result.data.records != null) {
                        initBannerView(result.data.records)
                    } else {
                    }
                }
                else -> {
                    XUtils.toast().show(result.msg)
                }
            }
        } else {
            return
        }
    }

    /**
     * 轮播图
     */
    private fun initBannerView(records: MutableList<ArticleEntity>) {
        val titles: ArrayList<String> = java.util.ArrayList()
        val bannerList: ArrayList<SlideshowListBean> = ArrayList()
        for (item in records) {
            titles.add(item.theme)
            bannerList.add(SlideshowListBean(item.id, item.imgUrl))
        }

        bannerView!!.setBannerTitles(titles)
            .setAutoPlay(true)
            .setPages(bannerList, CustomViewHolder())
            .setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE)
            .setBannerAnimation(Transformer.Default)
            .setDelayTime(3000)
            .start()

        bannerView!!.setOnBannerClickListener { _, position ->
            val id = bannerList[position].id
            if (TextUtils.isEmpty(id)) {
                return@setOnBannerClickListener
            }
            XUtils.intent().openActivity(this, ArticleDetailsActivity::class.java)
                .putStringExtra("id", bannerList[position].id)
                .start()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("全会精神")
        viewModel = ArticleViewModel()
        // 轮播图
        initViewListData("QHJS_LBXW")

        initTabView()
    }

    private fun initTabView() {
        val titleList = ArrayList<String>()
        val typeList = ArrayList<String>()

        titleList.add("党的十九届六中全会精神")
        titleList.add("省委十二届十次全会精神")
        titleList.add("市委七届十二次全会精神")
        titleList.add("评论解读")
        titleList.add("热议反响")

        typeList.add("QHJS_DDWZQH")
        typeList.add("QHJS_SBCQH")
        typeList.add("QHJS_SSECQH")
        typeList.add("QHJS_PLJD")
        typeList.add("QHJS_RYFX")

        initTab(
            titleList,
            getFragments(typeList)
        )
    }

    private fun getFragments(typeList: java.util.ArrayList<String>): List<Fragment> {
        val fragments: ArrayList<Fragment> = ArrayList()

        for (type in typeList) {
            val fragment = ArticleListFragment()
            val bundle = Bundle()
            bundle.putString("type", type)
            fragment.arguments = bundle
            fragments.add(fragment)
        }

        return fragments
    }

    private fun initTab(titles: ArrayList<String>, fragments: List<Fragment>) {

        val fragmentPagerAdapter = object : FragmentPagerAdapter(supportFragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(arg0: Int): Fragment {
                return fragments[arg0]
            }
        }

        viewPager.offscreenPageLimit = titles.size - 1
        viewPager.adapter = fragmentPagerAdapter
        // pager.addOnPageChangeListener(this);

        scrollTab.setTitles(titles, 0, false) //
//        scrollTab.setNumber(1, "9", View.VISIBLE)//设置数字红点
        scrollTab.setViewPager(viewPager)
        scrollTab.setOnTabListener { index, _ ->
            viewPager.setCurrentItem(index, true)
        }
    }
}